local Cell = {}
local cellSheetInfo = require('resources.sprites.atlasses.map')
local cellSheet = graphics.newImageSheet( 'resources/sprites/atlasses/map.png', cellSheetInfo:getSheet())
function Cell:new(x, y, w, h, gridX, gridY, cellConfig)
	local cell = nil
	if cellConfig.type == nil then
		cell = display.newRect(x, y, w, h)
		cell.alpha = 0
		cellConfig.type = ABISM
	else
		cell = display.newImage(cellSheet, cellSheetInfo:getFrameIndex(cellConfig.spriteId))
		cell:scale(4/9, 4/9)
		-- print (cellConfig.type, cellConfig.spriteId, cellConfig.appearence, gridX, gridY)
		--assert(false)
	end

	cell.x = x
	cell.y = y
	cell.w = w
	cell.h = h
	cell.gridRow = gridX
	cell.gridCol = gridY
	cell.requiredMask = 0
	cell.damage = 0
	cell.cellType = cellConfig.type
	cell.spriteId = cellConfig.spriteId
	cell.abismConfig = cellConfig.appearance

	function cell:getGridRow()
		return cell.gridRow
	end
	function cell:getGridCol()
		return cell.gridCol
	end

	local cellTypeBehavior = {}
	cellTypeBehavior[ABISM] = {
		damage = 1000000,
		requiredMask = NO_MASK,
		handler = function (player, delta)
			cell:damagePlayerFixed(player)
		end
	}
	cellTypeBehavior[EMPTY_CELL] = {
		damage = 1000000,
		requiredMask = NO_MASK,
		handler = function (player, delta)
		end
	}
	cellTypeBehavior[RED_CELL] = {
		damage = 150,
		requiredMask = BLUE_MASK,
		handler = function (player, delta)
			cell:damagePlayerWithNoMask(player, delta)
		end
	}
	cellTypeBehavior[PURPLE_CELL] = {
	damage = 150,
	requiredMask = RED_MASK,
	handler = function (player, delta)
		cell:damagePlayerWithNoMask(player, delta)
	end
	}
	cellTypeBehavior[GOAL] = {
	damage = 0,
	requiredMask = NO_MASK,
	handler = function (player, delta)
	end
	}
	cellTypeBehavior[ICE_CELL] = {
		damage = 0,
		requiredMask = NO_MASK,
		handler = function (player, delta)
			player:setSliding(true)
		end
	}
	cell.damage = cellTypeBehavior[cell.cellType].damage
	cell.requiredMask = cellTypeBehavior[cell.cellType].requiredMask


	function cell:setSprite()

	end
	function cell:getCellType()
		return self.cellType
	end

---	function cell:changeCellType(cellType)
--		self.cellType = cellType
--		self:setSprite(cellType)
		--self.modifyPlayerState = cellTypeBehavior[cellType].handler
--		self.requiredMask = cellTypeBehavior[cellType].requiredMask
--	end


	function cell:modifyPlayerState(player, delta)
		player:resetPlayerState()
		if self.abismConfig == 't' then
			cellTypeBehavior[cell.cellType]['handler'](player, delta)
		else
			local leftAbism = true
			if self.abismConfig == 'ad' then
				leftAbism = false
			end
			local ax, bx = self.x - self.w/2, self.x + self.w/2
			local cx, dx = ax, bx
			local ay, cy = self.y  - self.h/2, self.y + self.h/2
			local by, dy = ay, cy
			local px, py = player:getReferencePos()
			-- print (ax, ay, bx, by, cx, cy, px, py)
			local A, B, C, P = nil, Vector:new(bx, by), Vector:new(cx, cy), Vector:new(px, py)
			if leftAbism then
				A = Vector:new(ax, ay)
			else
				A = Vector:new(dx, dy)
			end
			if  GeometricUtils:pointInTriangle(P, A, B, C) == false then
				cellTypeBehavior[cell.cellType]['handler'](player, delta)
			else
				cell:killPlayer(player)
			end
		end

	end

	function cell:haunt2()
    audio.play(haunts2[math.random(#haunts2)],{channel=5})
  end

	function cell:damagePlayerWithNoMask(player, delta)
		self:haunt2()
		if player:getMask() ~= self.requiredMask then
			self:damagePlayer(player, delta)
			player:setBeingPisssh(true)
		end
	end
	function cell:damagePlayer (player, delta)
		player:damagePlayer(self.damage * delta)
		-- self:haunt()
	end
	function cell:damagePlayerFixed(player)
		print 'damaging fixed'
		player:damagePlayer(self.damage ,1)
		print 'after damagin fixed'
	end
	function cell:killPlayer(player)
		player:damagePlayer(1000, 1)
		audio.play(haunts3[math.random(#haunts3)],{channel=5})
	end

	return cell
end

return Cell